package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcUserBrowseHistory;
import com.ruoyi.fun.service.IEcUserBrowseHistoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户浏览历史Controller
 * 
 * @author ruoyi
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/system/browseHistory")
public class EcUserBrowseHistoryController extends BaseController
{
    @Autowired
    private IEcUserBrowseHistoryService ecUserBrowseHistoryService;

    /**
     * 查询用户浏览历史列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcUserBrowseHistory ecUserBrowseHistory)
    {
        startPage();
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecUserBrowseHistory.setOrgId(orgId);
        List<EcUserBrowseHistory> list = ecUserBrowseHistoryService.selectEcUserBrowseHistoryList(ecUserBrowseHistory);
        return getDataTable(list);
    }

    /**
     * 根据用户ID查询浏览历史列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:list')")
    @GetMapping("/user/{userId}")
    public TableDataInfo listByUserId(@PathVariable Long userId)
    {
        startPage();
        List<EcUserBrowseHistory> list = ecUserBrowseHistoryService.selectEcUserBrowseHistoryByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 根据用户ID和组织ID查询浏览历史列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:list')")
    @GetMapping("/user/{userId}/org/{orgId}")
    public TableDataInfo listByUserIdAndOrgId(@PathVariable Long userId, @PathVariable Long orgId)
    {
        startPage();
        List<EcUserBrowseHistory> list = ecUserBrowseHistoryService.selectEcUserBrowseHistoryByUserIdAndOrgId(userId, orgId);
        return getDataTable(list);
    }

    /**
     * 导出用户浏览历史列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:export')")
    @Log(title = "用户浏览历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcUserBrowseHistory ecUserBrowseHistory)
    {
        List<EcUserBrowseHistory> list = ecUserBrowseHistoryService.selectEcUserBrowseHistoryList(ecUserBrowseHistory);
        ExcelUtil<EcUserBrowseHistory> util = new ExcelUtil<EcUserBrowseHistory>(EcUserBrowseHistory.class);
        util.exportExcel(response, list, "用户浏览历史数据");
    }

    /**
     * 获取用户浏览历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:history:query')")
    @GetMapping(value = "/{browseId}")
    public AjaxResult getInfo(@PathVariable("browseId") Long browseId)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        return success(ecUserBrowseHistoryService.selectEcUserBrowseHistoryByBrowseIdAndOrgId(browseId, orgId));
    }

    /**
     * 根据浏览ID和组织ID获取用户浏览历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:history:query')")
    @GetMapping(value = "/{browseId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("browseId") Long browseId, @PathVariable("orgId") Long orgId)
    {
        return success(ecUserBrowseHistoryService.selectEcUserBrowseHistoryByBrowseIdAndOrgId(browseId, orgId));
    }

    /**
     * 获取今日浏览历史（按组织ID）
     */
    @PreAuthorize("@ss.hasPermi('system:history:list')")
    @GetMapping("/today/user/{userId}/product/{productId}/org/{orgId}")
    public TableDataInfo getTodayBrowseHistoryByOrgId(@PathVariable Long userId, @PathVariable Long productId, @PathVariable Long orgId)
    {
        startPage();
        EcUserBrowseHistory result = ecUserBrowseHistoryService.selectTodayBrowseHistoryByOrgId(userId, productId, orgId);
        List<EcUserBrowseHistory> list = result != null ? java.util.Arrays.asList(result) : new java.util.ArrayList<>();
        return getDataTable(list);
    }

    /**
     * 新增用户浏览历史
     */
    @PreAuthorize("@ss.hasPermi('system:history:add')")
    @Log(title = "用户浏览历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcUserBrowseHistory ecUserBrowseHistory)
    {
        ecUserBrowseHistory.setOrgId(getOrgId());
        return toAjax(ecUserBrowseHistoryService.insertEcUserBrowseHistory(ecUserBrowseHistory));
    }

    /**
     * 修改用户浏览历史
     */
    @PreAuthorize("@ss.hasPermi('system:history:edit')")
    @Log(title = "用户浏览历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcUserBrowseHistory ecUserBrowseHistory)
    {
        return toAjax(ecUserBrowseHistoryService.updateEcUserBrowseHistory(ecUserBrowseHistory));
    }

    /**
     * 删除用户浏览历史
     */
    @PreAuthorize("@ss.hasPermi('system:history:remove')")
    @Log(title = "用户浏览历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{browseIds}")
    public AjaxResult remove(@PathVariable Long[] browseIds)
    {
        return toAjax(ecUserBrowseHistoryService.deleteEcUserBrowseHistoryByBrowseIds(browseIds));
    }
}